package rtcf.utils
{
	import flash.events.AsyncErrorEvent;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.messaging.messages.AsyncMessage;
	import mx.messaging.messages.ErrorMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ConfigLoader extends EventDispatcher
	{

		private var configLoader:URLLoader;
		private var configRequest:URLRequest;
		public var configXML:XML;
		
		public function ConfigLoader()
		{
			
		}
		
		public function loadConfig(configURI:String = null):void{
			if (!configURI) configURI = "config.xml";
			// Get the configuration file
			configLoader = new URLLoader();
			// Appending a random number insures we get the latest config file each time.
			configRequest = new URLRequest(configURI + "?r=" + Math.floor(Math.random()*1000000000000000));
			configLoader.addEventListener("complete", configLoaded);
			configLoader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			configLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
			configLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			try{
				configLoader.load(configRequest);
			}
			catch (error:Error){
				dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false,false,"Can't load config.xml initialization file.\n" + error.message));
			}			
			
		}
		
		private function clear():void{
			configLoader.removeEventListener("complete", configLoaded);
			configLoader.removeEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			configLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
			configLoader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);	
			configLoader.close();
		}
		
		
		private function configLoaded(event:Event):void{
			clear();
			configXML = XML(configLoader.data);
			// flash.system.Security.loadPolicyFile(configXML.webApplication.@gatewayURI.split("/").slice(0, 3).join("/") + "/crossdomain.xml");
			dispatchEvent(new Event(Event.COMPLETE, true, true));
		}
		
		private function progressHandler(event:ProgressEvent):void{
			//progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
			// trace("bytes loaded: " + event.bytesLoaded + ", bytes total: " + event.bytesTotal)
		}
		
		private function handleIOError(event:IOErrorEvent):void{
			//trace("StartupManager.handleIOError> event: " + event);
			//("StartupManager.handleIOError> event.text: " + event.text);
			clear();
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, true, true, event.text));
		}
		
		private function handleSecurityError(event:SecurityErrorEvent):void{
			//trace("StartupManager.handleSecurityError> event: " + event);
			//trace("StartupManager.handleSecurityError> event.text: " + event.text);
			clear();
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, true, true, event.text));
		}		
	}
}